package com.tianheng.modbus.nettyservice;

import com.tianheng.modbus.dao.ModbusDeviceRepository;
import com.tianheng.modbus.dto.DeviceAndOrderDTO;
import com.tianheng.modbus.entity.ModbusDevice;
import com.tianheng.modbus.entity.ModbusDeviceTemplateProperty;
import com.tianheng.modbus.util.CRC16;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**************************************************
 * copyright (c) 2020, peng.cao@tianheng-uestc.com All Rights Reserved.
 * created by peng.cao
 * date:       2020/7/9 0009
 * since:      1.0.0.1
 * description:
 *
 **************************************************/
@Service("modbusService")
public class ModbusDeviceServiceImpl  implements  ModbusDeviceService{

    @Autowired
    private ModbusDeviceRepository modbusDeviceRepository;


    @Override
    public ModbusDevice getDevice(Long id) {
        Optional<ModbusDevice> modbusDevice = modbusDeviceRepository.findById(id);
        return modbusDevice.get();
    }


    @Override
    public  String  getOrder(Long id){
        ModbusDevice device = getDevice(id);
        StringBuilder stringBuilder = new StringBuilder();
        ModbusDeviceTemplateProperty modbusDeviceTemplateProperty = new ModbusDeviceTemplateProperty();
        device.getModbusDeviceTemplate().getPropertys().forEach(item->{
            BeanUtils.copyProperties(item,modbusDeviceTemplateProperty);

        });
        int slaveId=1;
        String code="00";
        Integer startAddress=0;
        Integer segmentQty=0;
        if(modbusDeviceTemplateProperty.getId()!=null) {
             slaveId = device.getSlaveId().intValue();
             code = modbusDeviceTemplateProperty.getFunctionCode().getCode();

             startAddress = modbusDeviceTemplateProperty.getStartAddress();
             segmentQty = modbusDeviceTemplateProperty.getSegmentQty();
        }else {

        }

       String res = stringBuilder.append(tenExchange(2,slaveId)).append(code).append(tenExchange(4,startAddress)).append(tenExchange(4,segmentQty)).toString();
        String crc = CRC16.getCRC(res);

        String result =stringBuilder.append(crc).toString();
        return result;

    }

    @Override
    public List<DeviceAndOrderDTO> getDeviceAndOrder() {
        List<ModbusDevice> modbusDevices = modbusDeviceRepository.findAll();
       List<DeviceAndOrderDTO> deviceAndOrderDTOS = new ArrayList<>();
        modbusDevices.forEach(item->{
            DeviceAndOrderDTO deviceAndOrderDTO = new DeviceAndOrderDTO();
            deviceAndOrderDTO.setModbusDevice(item);
           deviceAndOrderDTO.setOrder(getOrder(item.getId()));
            deviceAndOrderDTOS.add(deviceAndOrderDTO);

        });
        return deviceAndOrderDTOS;
    }


    /**
     * 10进制转16
     * @param valueTen
     * @return
     */
    public static String tenExchange(int num, int valueTen){
        return String.format("%0"+num+"x",valueTen);
    }


    public static void main(String[] args) {
        String s = tenExchange(4, 121);
        System.out.println(s);
    }
}
